document.addEventListener('DOMContentLoaded', function() {
    const slidesWrapper = document.querySelector('.slides-wrapper');
    const carouselContainer = document.querySelector('.carousel-container'); // 新增
    const indicators = document.querySelectorAll('.indicator');
    
    let currentSlide = 0;
    const totalSlides = document.querySelectorAll('.slide').length;
    
    // 触摸/鼠标滑动相关变量
    let startX = 0;
    let currentX = 0;
    let isDragging = false;
    
    // 设置初始容器高度
    updateContainerHeight();
    
    // 切换到特定幻灯片
    function goToSlide(index) {
        if (index < 0 || index >= totalSlides) return;
        
        currentSlide = index;
        slidesWrapper.style.transform = `translateX(-${currentSlide * 100}%)`;
        
        // 更新指示器
        updateIndicators();
        
        // 更新容器高度
        updateContainerHeight();
    }
    
    // 更新容器高度 - 修改这里
    function updateContainerHeight() {
        const currentSlideElement = document.querySelectorAll('.slide')[currentSlide];
        const newHeight = currentSlideElement.offsetHeight;
        // 修改为更新carousel-container的高度而不是slides-wrapper
        carouselContainer.style.height = `${newHeight}px`;
        // 移除下面这行
        // slidesWrapper.style.height = `${newHeight}px`;
    }
    
    // 更新指示器状态
    function updateIndicators() {
        indicators.forEach((indicator, i) => {
            const indicatorIndex = parseInt(indicator.getAttribute('data-index'));
            indicator.classList.toggle('active', indicatorIndex === currentSlide);
        });
    }
    
    // 事件监听器 - 指示器点击
    indicators.forEach(indicator => {
        indicator.addEventListener('click', function(e) {
            e.stopPropagation();
            const slideIndex = parseInt(this.getAttribute('data-index'));
            goToSlide(slideIndex);
        });
    });
    
    // 响应窗口大小变化
    window.addEventListener('resize', updateContainerHeight);
    
    // 触摸滑动功能
    slidesWrapper.addEventListener('touchstart', handleStart, { passive: true });
    slidesWrapper.addEventListener('touchmove', handleMove, { passive: true });
    slidesWrapper.addEventListener('touchend', handleEnd);
    
    // 鼠标滑动功能（用于测试）
    slidesWrapper.addEventListener('mousedown', handleStart);
    window.addEventListener('mousemove', handleMove);
    window.addEventListener('mouseup', handleEnd);
    
    function handleStart(e) {
        if (e.type === 'mousedown') {
            startX = e.clientX;
            e.preventDefault();
        } else if (e.type === 'touchstart') {
            startX = e.touches[0].clientX;
        }
        
        isDragging = true;
        slidesWrapper.style.transition = 'none';
    }
    
    function handleMove(e) {
        if (!isDragging) return;
        
        if (e.type === 'mousemove') {
            currentX = e.clientX;
        } else if (e.type === 'touchmove') {
            currentX = e.touches[0].clientX;
        }
        
        const diff = currentX - startX;
        
        // 限制滑动范围
        if ((currentSlide === 0 && diff > 0) || (currentSlide === totalSlides - 1 && diff < 0)) {
            return;
        }
        
        // 实时移动轮播
        slidesWrapper.style.transform = `translateX(calc(-${currentSlide * 100}% + ${diff}px))`;
    }
    
    function handleEnd() {
        if (!isDragging) return;
        
        isDragging = false;
        // 修改过渡效果，移除height过渡
        slidesWrapper.style.transition = 'transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94)';
        
        const diff = currentX - startX;
        const threshold = 50; // 滑动阈值
        
        if (diff > threshold && currentSlide > 0) {
            // 向右滑动，切换到上一页
            goToSlide(currentSlide - 1);
        } else if (diff < -threshold && currentSlide < totalSlides - 1) {
            // 向左滑动，切换到下一页
            goToSlide(currentSlide + 1);
        } else {
            // 回到当前位置
            goToSlide(currentSlide);
        }
    }
});